package com.haoniu.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.aliyuncs.exceptions.ClientException;
import com.haoniu.auth.utils.SMSUtils;
import com.haoniu.auth.utils.ValidateCodeUtils;
import com.haoniu.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.haoniu.auth.form.LoginBody;
import com.haoniu.auth.form.RegisterBody;
import com.haoniu.auth.service.SysLoginService;
import com.haoniu.common.core.domain.R;
import com.haoniu.common.core.utils.JwtUtils;
import com.haoniu.common.core.utils.StringUtils;
import com.haoniu.common.security.auth.AuthUtil;
import com.haoniu.common.security.service.TokenService;
import com.haoniu.common.security.utils.SecurityUtils;
import com.haoniu.system.api.model.LoginUser;

import java.util.concurrent.TimeUnit;

/**
 * token 控制
 *
 * @author haoniu
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @PostMapping("tel_login")
    public R<?> tel_login(String tel,String code)
    {
        System.err.println("tel is " + tel);
        System.err.println("code is " + code);
        // 用户登录
        LoginUser userInfo = sysLoginService.tel_login(tel,code);
        // 获取登录token
        return R.ok("登录成功");
    }

    @PostMapping("code")
    public R<?> getCode(String tel) throws ClientException {
        String code = ValidateCodeUtils.generateValidateCode(4) + "";
        System.err.println("手机号: " + tel);
        System.err.println("验证码: " + code);
        redisTemplate.opsForValue().set(tel,code,5, TimeUnit.MINUTES);
        return SMSUtils.sendShortMessage("SMS_154950909",tel,code);
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }
}
